################################################################################
# Board: MKS Robin Nano v1.1 (Flying Bear Reborn 2.0) / MKS Robin Nano v1.3 / MKS Robin Nano-S v1.3
# Printer: Flying Bear Ghost 5
# Config by ODUCCEU
################################################################################
# WARNING! DO NOT EDIT THIS FILE!
# To override settings from this file, you can copy and paste the relevant
# sections into your printer.cfg and change it there.
################################################################################

# https://www.klipper3d.org/BLTouch.html
# Most BL-Touch devices require a pullup on the sensor pin (prefix the pin name with "^")

# Offset calculator
# https://docs.google.com/spreadsheets/d/1RAdhZYmozszxVk1wzSGsmHUTGrgdN-qGp5gZ5-Ksxr8/edit?usp=sharing

# disable 'position_endstop' in [stepper_z] section of printer.cfg

################################################################################
# Pin Definitions
################################################################################

# You have different choices on how to plug your probe on MKS Robin Nano:
# 
# Sensor can be plugged into one of the list:
## Z_MIN_STOP           PA11
## Z_MAX_STOP           PC4
## MT_DET2              PE6
## WiFi_IO0             P13     MKSRNv1.3
## WiFi_IO0             PA8     MKSRNv1.1
# 
# Servo control can be plugged into one of the list:
## SERVO                PA8     'BLTOUCH' slot on MKSRNv1.3
## PB2                  PB2
# 
# You can check default 'BLTOUCH_SENSOR_PIN' and 'BLTOUCH_CONTROL_PIN' in your board cfg file

################################################################################
# BLTouch Probe
################################################################################

[bltouch]
sensor_pin: ^BLTOUCH_SENSOR_PIN
control_pin: BLTOUCH_CONTROL_PIN
# pin_up_reports_not_triggered: True
#   Set if the BLTouch consistently reports the probe in a "not
#   triggered" state after a successful "pin_up" command. This should
#   be True for all genuine BLTouch devices. Read the directions in
#   docs/BLTouch.md before setting this to False. The default is True.
# Config ref: https://www.klipper3d.org/Config_Reference.html?h=pin_up_reports_not_triggered#bltouch
# 

# stow_on_each_sample: False                                                      # faster but dangerous
x_offset: 44                                                                      # CAUTION! Set your own offset
y_offset: 1                                                                       # CAUTION! Set your own offset
z_offset: 2.350                                                                   # CAUTION! Set your own offset
speed: 12
samples: 3

################################################################################
# Homing Z
################################################################################

[stepper_z]
endstop_pin: probe:z_virtual_endstop
position_min: -5

[safe_z_home]
# home_xy_position: 127.5,105                                                     # SET NOZZLE XY = probe_xy - offset_xy
home_xy_position: 76,104
speed: 100
z_hop: 10
z_hop_speed: 12
# move_to_previous: True                                                          # return back to previous X/Y after Z-home

################################################################################
# Bed leveling
################################################################################

[bed_mesh]
speed: 20
horizontal_move_z: 10
mesh_min: 49,4                                                                   # CAUTION! = probe_xy = nozzle_xy + offset_xy
mesh_max: 240,210                                                                # CAUTION! = probe_xy = nozzle_xy + offset_xy
probe_count: 7,5
mesh_pps: 3
algorithm: bicubic
# relative_reference_index: 18                                                    # center point

# Затухание эффекта прогрммного вырванивания стола. 
# Начинается со слоя № (1) и полностюся завершается на слое № (10)
# fade_start: 1  
# fade_end: 10
# fade_target: 0

[screws_tilt_adjust]                                                            # = nozzle_xy = probe_xy - offset_xy

# By Google sheets calculation. Small overalp, so I decreased it 
# https://docs.google.com/spreadsheets/d/1oZJ7Ny1GvRJ69KNQO_VR_iY7pfzSLl1n_EZKfyBdsTM/edit?usp=sharing
# screw1: 1,41.5
# screw1_name: front left screw
# screw2: 240,41.5
# screw2_name: front right screw
# screw3: 240,210
# screw3_name: back right screw
# screw4: 1,210
# screw4_name: back left screw
# speed: 100
# horizontal_move_z: 10
# screw_thread: CW-M4

# Initial from FBG5 config 
# screw1: 25,30
screw1: 25,41.5
screw1_name: front left screw
screw2: 230,41.5
screw2_name: front right screw
screw3: 230,180
screw3_name: back right screw
screw4: 25,180
screw4_name: back left screw
speed: 100
horizontal_move_z: 10
screw_thread: CW-M4


################################################################################
# Macros
################################################################################

[gcode_macro G29]                                                               # Bed Levelling (Automatic)
gcode:
    {% if printer.toolhead.homed_axes != "xyz" %} G28 {% endif %}               # home if not homed
    BED_MESH_CALIBRATE
    G0 X0 Y0 F6000

[gcode_macro PROBE_CALIBRATE]                                                   # Calibrate Z-offset
rename_existing: BASE_PROBE_CALIBRATE
gcode:
    {% if printer.toolhead.homed_axes != "xyz" %} G28 {% endif %}               # home if not homed
    BASE_PROBE_CALIBRATE

## Replace START_PRINT macro ###################################################

[gcode_macro START_PRINT]
## PrusaSliser: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={first_layer_temperature[0]} BED_TEMP={first_layer_bed_temperature} MESH_DO=<1|0>
## Cura: 
# M190 S0
# M109 S0
# START_PRINT EXTRUDER_TEMP={material_print_temperature_layer_0} BED_TEMP={material_bed_temperature_layer_0} MESH_DO=<1|0>
description: Start routine for the print
variable_retract: 10
gcode:
    {% set extruder_temp = params.EXTRUDER_TEMP|default(240)|float %}
    {% set bed_temp = params.BED_TEMP|default(70)|float %}
    {% set E = printer["gcode_macro START_PRINT"].retract|float %}
    {% set mesh = params.MESH_DO|default(0)|int %}
    CLEAR_PAUSE
    M220 S100                                                                   # reset feedrate
    M221 S100                                                                   # reset flowrate
    G90                                                                         # absolute positioning
    M82                                                                         # relative extrusion mode
    SET_HEATER_TEMPERATURE HEATER=heater_bed TARGET={bed_temp}                  # set bed temp
    TEMPERATURE_WAIT SENSOR=heater_bed MINIMUM={bed_temp}                       # wait for bed temp
    SET_HEATER_TEMPERATURE HEATER=extruder TARGET={extruder_temp}               # set extruder temp
    G28                                                                         # home
    {% if 'MESH_DO' in params %}
        {% if mesh == 0 %}
            BED_MESH_PROFILE LOAD={bed_temp}                                    # load existing mesh
        {% else %}
            G29                                                                 # do G29 while heating extruder
            BED_MESH_PROFILE SAVE={bed_temp}
        {% endif %}
    {% endif %}
    TEMPERATURE_WAIT SENSOR=extruder MINIMUM={extruder_temp}                    # wait for extruder temp
    G0 Z10 F1500                                                                # raise Z
    G92 E0                                                                      # reset extruder
    G1 E{E} F1500                                                               # prime
    G92 E0                                                                      # reset extruder

################################################################################
